package com.edz.dudumifi.event;

import android.content.Context;
import android.util.Log;

import com.edawtech.module.AskMsg;
import com.edawtech.module.AskParams;
import com.edz.NettyClientBootstrap;
import com.edz.dudumifi.base.Config;
import com.edz.dudumifi.utils.KKTcpClient;
import com.edz.dudumifi.utils.PreferencesUtils;

import java.util.concurrent.TimeUnit;

import io.netty.channel.socket.SocketChannel;

/**
 * Created by ganshh.gan on 2018/5/16.
 */
public class SendSystemInfioThread extends Thread {
    private static final String TAG = "SendSystemInfioThread";
    private String cmd;
    private Context context;

    public SendSystemInfioThread(Context context, String cmd) {
        this.cmd = cmd;
        this.context = context;
    }

    @Override
    public void run() {
        super.run();
        String imei = null;
        SocketChannel channel = null;

        while (true) {
            imei = PreferencesUtils.getString(context, Config.IMEI);

            if (imei != null)
                channel = NettyClientBootstrap.getSocketChannel();

            if (channel != null)
                break;
        }

        String strRtn = null;
        while (true) {

            strRtn = KKTcpClient.Instance().RequestGateway("127.0.0.1", cmd);
            sendMsg(strRtn);

            try {
                TimeUnit.MINUTES.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }

    private void sendMsg(String msg) {
        Log.i(TAG, "发送数据" + msg);
        AskMsg askMsg = new AskMsg();
        AskParams askParams = new AskParams();
        askParams.setContent(msg);
        askMsg.setParams(askParams);
        NettyClientBootstrap.getBootstrap().getSocketChannel().writeAndFlush(askMsg);
    }
}
